import { TextDecoder } from 'text-encoding';
//判断是否json字符串
const isJSON = (str) => {
  if (typeof str == 'string') {
    try {
      var obj = JSON.parse(str);
      if (typeof obj == 'object' && obj) {
        return true;
      } else {
        return false;
      }
    } catch (e) {
      return false;
    }
  }

}
//request响应字符串处理
export const arrayBufferToString = (buffer) => {
  const decoder = new TextDecoder("utf-8"); // 假设数据是 UTF-8 编码
  let text = decoder.decode(buffer)
  let arr = text.split("data:")
  let newArr = []
  arr.forEach(item => {
    if (item && isJSON(item)) {
      newArr.push(JSON.parse(item))
    }
  })
  return newArr
}


